﻿using System;
using System.Collections.Generic;
using System.Linq;
using IterationLife.Domains.Sports.Configs;
using IterationLife.Domains.Sports.Dtos.MTSports;
using IterationLife.Domains.Sports.Dtos.ZeppSports;
using IterationLife.Domains.Sports.Enums;
using IterationLife.Domains.Sports.Models;
using IterationLife.Domains.Sports.Repositories;
using Util.Domains;
using Util.Exceptions;
using Util.Helpers;
using Web = IterationLife.Extensions.HttpRequest.Web;

namespace IterationLife.Domains.Sports.Services
{
    public class LoginManager : DomainServiceBase, ILoginManager
    {
        /// <summary>
        /// 构造
        /// </summary>
        public LoginManager(ISportUserRepository sportUserRepository, ISportAthleteRepository sportAthleteRepository, IDataImportManager dataManager, IMTDataSerializable mtDataSerializable, IZeppDataSerializable zeppDataSerializable)
        {
            SportUserRepository = sportUserRepository;
            SportAthleteRepository = sportAthleteRepository;
            DataManager = dataManager;
            MTDataSerializable = mtDataSerializable;
            ZeppDataSerializable = zeppDataSerializable;
        }

        protected ISportUserRepository SportUserRepository { get; set; }
        protected ISportAthleteRepository SportAthleteRepository { get; set; }
        protected IDataImportManager DataManager { get; set; }
        protected IMTDataSerializable MTDataSerializable { get; set; }
        protected IZeppDataSerializable ZeppDataSerializable { get; set; }
        /// <summary>
        /// 登录用户
        /// </summary>
        public SportUser Login(string userName, string password, EquipmentType equipmentType)
        {
            switch (equipmentType)
            {
                case EquipmentType.MT:
                    return MTLogin(userName, password);
                case EquipmentType.Zepp:
                    return ZeppLogin(userName, password);

            }
            throw new Warning("设备未支持");
        }

        #region 动量登录

        /// <summary>
        /// 动量登录
        /// </summary>
        private SportUser MTLogin(string userName, string password)
        {
            //加密用户密码
            var loginEncoderJson = Web.HttpGet(MTUrlConfig.UserNameAndPasswordEncode, new Dictionary<string, string>() { { "type", "password" }, { "userName", userName }, { "password", password } });
            var loginEncoder = Util.Json.ToObject<MTHttpResult<LoginParams>>(loginEncoderJson);
            //登录动量科技
            var userInfoJson = Web.HttpPost(MTUrlConfig.Login, new Dictionary<string, string>() { { "userName", loginEncoder.data.userName }, { "password", loginEncoder.data.password }, { "loginType", "1" } });
            var userInfo = Util.Json.ToObject<MTHttpResult<MTUserInfo>>(userInfoJson);
            if (userInfo.data.personId == null || userInfo.data.uid == null)
                throw new Warning("登录失败");
            //存储到我们服务器
            return MTDataSerializable.ImportUser(userInfo.data.uid);
        }

        #endregion

        #region Zepp登录

        /// <summary>
        /// Zepp登录
        /// </summary>
        private SportUser ZeppLogin(string userName, string password)
        {
            if (Util.Helpers.Regex.IsMatch(userName, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"))
                return ZeppEmailLogin(userName, password);
            else
                return ZeppPhoneLogin(userName, password);
        }

        private SportUser ZeppPhoneLogin(string phomeNumber, string password)
        {
            var loginJson = Web.HttpPost(ZeppUrlConfig.PhoneLogin, new Dictionary<string, string>() { { "phoneNumber", phomeNumber }, { "password", password } });
            var login = Util.Json.ToObject<ZeppHttpResult<ZeppLogin>>(loginJson);
            if (string.IsNullOrWhiteSpace(login.result.user.id))
                throw new Warning("登录失败");
            //存储到我们服务器
            return ZeppDataSerializable.ImportUser(login.result.user);
        }

        private SportUser ZeppEmailLogin(string email, string password)
        {
            return null;
        }

        #endregion
    }
}
